package hong.jun.task;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;

/**
 * 说明:
 * Created on 2023/10/19 15:50
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class Main {

    public static void main(String[] args) {
        TaskThreadPoolManger instance = TaskThreadPoolManger.getInstance();

        //instance.addSingleTask(new ThreadFightTask("玩家1","玩家2"));
        //instance.addSingleTask(new ThreadFightTask("玩家3","玩家4"));
        //instance.addSingleTask(new ThreadFightTask("玩家5","玩家6"));

        //instance.addFixedTask(new ThreadFightTask("玩家1", "玩家2"));
        //instance.addFixedTask(new ThreadFightTask("玩家3", "玩家4"));
        //instance.addFixedTask(new ThreadFightTask("玩家5", "玩家6"));

        ThreadFightTask threadFightTask1 = new ThreadFightTask("玩家1", "玩家2");
        //threadFightTask1.setTask1(() -> othertask());
        threadFightTask1.setTask1(Main::othertask);
        threadFightTask1.addSingleTask();

        /*try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 100; i++) {
                ThreadFightTask threadFightTask = new ThreadFightTask("玩家甲", "玩家乙");
                executorService.submit(threadFightTask);
                //instance.addVirtualTask(threadFightTask);
            }
        }*/
        long time1 = System.currentTimeMillis();
        List<CompletableFuture<Object>> collect = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            ThreadFightTask threadFightTask = new ThreadFightTask("玩家甲", "玩家乙");
            /*threadFightTask.setTask1(new Runnable() {//匿名内部类设置线程任务
                @Override
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + "执行扩展任务");
                }
            });*/
            threadFightTask.setTask1(Main::othertask);//lambda表达式设置线程任务
            CompletableFuture<Void> future = CompletableFuture.runAsync(threadFightTask::action, Executors.newVirtualThreadPerTaskExecutor());//虚拟线程池
            //CompletableFuture<Void> future = CompletableFuture.runAsync(() -> threadFightTask.action(), instance.getFixedThreadPool()); //多线程池线程
            //CompletableFuture<Void> future = CompletableFuture.runAsync(() -> threadFightTask.action(), instance.getSingleThreadPool()); //单线程池线程
            CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(future);
            collect.add(objectCompletableFuture);
        }
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(collect.toArray(new CompletableFuture[0]));//列表里所有线程执行完才放行
        voidCompletableFuture.join();
        long time2 = System.currentTimeMillis();
        //System.out.println(String.format("执行耗时%s", time2 - time1));
        System.out.printf("执行耗时%s%n", time2 - time1);

    }

    public static void othertask() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + "执行扩展任务");
    }

}
